package com.car.action.api;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.car.action.util.CommonController;
import com.car.common.model.BlackList;
import com.car.common.model.Card;
import com.car.common.model.CardCollect;
import com.car.common.model.CardDiscuss;
import com.car.common.model.CardHot;
import com.car.common.model.CardImg;
import com.car.common.model.CardLike;
import com.car.common.model.CardTopic;
import com.car.common.model.CircleLike;
import com.car.common.model.MessageUnreadNum;
import com.car.common.model.ReportCards;
import com.car.common.model.Social;
import com.car.common.model.UserBlack;
import com.car.common.model.UserFans;
import com.car.common.model.Users;
import com.car.common.model.VipRecord;
import com.car.job.TheadImgCheck;
import com.dvzju.lotto.k3.Rand;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.ehcache.IDataLoader;
import com.utils.APIJSON;
import com.utils.PicUtil;
import com.utils.RongCloudUtil;
import com.utils.StringUtil;
import com.utils.SystemClock;
import com.utils.Toolkit;
import com.vdurmont.emoji.EmojiParser;

public class CardController extends CommonController {

	/**
	 * 推荐帖子 
	 */
	public void sel_time() {
		Long user_id = super.getUserId();
		int page = 1;
        if(!StringUtil.empty(getPara("page"))) {
        	page = getParaToInt("page");
        }
        String cache_key = "card__sel_time:"+ user_id + "__" + page;
        ArrayList<Map<String,Object>> result = CacheKit.get("api", cache_key);
        
        if (result == null) {
        	result = Card.dao.sel_time(user_id , page);
        	CacheKit.put("api", cache_key, result);
        }
        renderJson(APIJSON.sendJSON(result));
	}
	
	/**
	 *   最新的
	 */
	public void sel_topic() {
		int page = 1;
        if(!StringUtil.empty(getPara("page"))) {
        	page = getParaToInt("page");
        }
        int last_id = 0;
        if(!StringUtil.empty(getPara("last_id"))) {
        	last_id = getParaToInt("last_id");
        }
        int type = 0;
        if(!StringUtil.empty(getPara("type"))) {
        	type = getParaToInt("type");
        }
        Long user_id = super.getUserId();
        ArrayList<Map<String,Object>> result = Card.dao.sel_topic(user_id , page, type, last_id);
        renderJson(APIJSON.sendJSON(result));
	}
	
	
	/**
	 *  本周 最热
	 */
	public void week_newest() {
        ArrayList<Map<String,Object>> result = Card.dao.week_newest(getUserId());
        renderJson(APIJSON.sendJSON(result));
	}
	
	/**
	 *  关注
	 */
	public void follow() {
		int page = 1;
        if(!StringUtil.empty(getPara("page"))) {
        	page = getParaToInt("page");
        }
        Long user_id = super.getUserId();
        ArrayList<Map<String,Object>> result = Card.dao.follow(page,user_id );
        renderJson(APIJSON.sendJSON(result));
	}
	/**
	 * 发帖
	 * @param topic_id
	 * @param content
	 * @param token
	 * @param images
	 */
	@Before(Tx.class)
	public void into_card() {
		String message = check_user(true);
		if (!StringUtil.empty(message)) {
			renderText(message);
		} else {
			String[] topic = null;
			String topic_id = getPara("topic_id");
			if (StringUtil.empty(topic_id)) {
				/*
				renderText(APIJSON.result(2002));
				return;
				*/
			} else {
				if (topic_id.endsWith(",")) {
					topic_id = topic_id.substring(0, topic_id.length() -1);
				}
				// 超过标签限制
				topic = topic_id.split(",");
				if (topic.length > 3) {
					renderText(APIJSON.result(1049));
				}
			}
			String content =  getPara("content");
			
			if (StringUtil.empty(content)) {
				renderText(APIJSON.result(2001));
				return;
			}
//			content = EmojiParser.parseToAliases(content); 
			
			String ids = getPara("ids");
			String[] atid = null;
			if (!StringUtil.empty(ids)) {
				if (ids.endsWith(",")) {
					ids = ids.substring(0, ids.length() -1);
				}
				atid = ids.split(",");
			}
			
			String images = getPara("images");
			String[] arr_images = null;
			if (!StringUtil.empty(images)) {
				if (images.endsWith(",")) {
					images = images.substring(0, images.length() -1);
				}
				arr_images = images.split(",");
			}
			Long user_id = super.getUserId();
			//频繁发帖
			Card last = Card.dao.findFirst("select * from card where user_id = " + user_id  +" order by id desc limit 1 ");
			if (last != null) {
				if (SystemClock.now()/1000L - last.getCreateTime() < 60) {
					renderJson(APIJSON.result(3005));
					return;
				}
			}
			Users users = Users.dao.findFirst("select forbid_time,user_mobile,is_true from users where id = " + user_id);
			if (users != null ) {
				long forbid_time = users.getLong("forbid_time");
				if (forbid_time > 0 && forbid_time < SystemClock.now()/1000L) {
					renderJson(APIJSON.result(1080));
					return;
				} 
				
				// 注册用户判断是否有黑名单词，水军pass
				if (users.getIsTrue() == 1){
					content = BlackList.dao.replace_content(content);
				} 
				/*
				if(StringUtil.empty(users.getUserMobile())) {
					renderJson(APIJSON.result(3006));
					return;
				}
				*/
			} 
			
			Card one = new Card();
			one.setIds(ids);
			one.setTopicId(topic_id);
			one.setIsCheck(0);
			one.setIsShow(2);
			one.setUserId(user_id.intValue());
			one.setCreateTime(SystemClock.now()/1000L);
        	one.setUpdateTime(SystemClock.now()/1000L);
        	one.setContent(StringUtil.encode(content));
        	if (users.getIsTrue() > 1){
				one.setImgCheck(1);
				one.setIsShow(1);
				one.setIsCheck(1);
			}
			if (one.save()) {
				Long card_id = one.getId();
				if (topic != null) {
					for(String t :topic) {
						CardTopic ctopic = new CardTopic();
						ctopic.setTopicId(Integer.valueOf(t));
						ctopic.setCardId(card_id.intValue());
						ctopic.setCreateTime(SystemClock.now()/1000L);
						ctopic.setUpdateTime(SystemClock.now()/1000L);
						ctopic.save();
					}
					
				}
				if (atid != null) {
					for(String t :atid) {
						MessageUnreadNum.dao.addNum(MessageUnreadNum.MESSAGE_UNREAD_TYPE_REMIND,Integer.valueOf(t));
					}
				}
				if (arr_images != null) {
					for(String t :arr_images) {
						CardImg img = new CardImg();
						img.setCardId(card_id.intValue());
						img.setImageUrl(t);
						img.setCreateTime(SystemClock.now()/1000L);
						img.setUpdateTime(SystemClock.now()/1000L);
						if (users.getIsTrue() == 2){
							img.setImgCheck(1);
						}
						img.save();
					}
				}
				if (users.getIsTrue() == 1){
					Map<String,Object> handleData = Maps.newHashMap(); 
					handleData.put("relation_id", one.getId());
					handleData.put("img", arr_images);
					handleData.put("type", 1);
					new Thread(new TheadImgCheck(handleData)).start();
				}
				 
			}
			renderText(APIJSON.result(0));
			
		}
	}
	
	
	public void copy_card() {
			String[] topic = null;
			String topic_id = getPara("topic_id");
			if (StringUtil.empty(topic_id)) {
			} else {
				if (topic_id.endsWith(",")) {
					topic_id = topic_id.substring(0, topic_id.length() -1);
				}
				// 超过标签限制
				topic = topic_id.split(",");
				if (topic.length > 3) {
					renderText(APIJSON.result(1049));
				}
			}
			String content =  getPara("content");
			
			if (StringUtil.empty(content)) {
				renderText(APIJSON.result(2001));
				return;
			}
			content = EmojiParser.parseToAliases(content); 
			
			String images = getPara("images");
			String[] arr_images = null;
			if (!StringUtil.empty(images)) {
				if (images.endsWith(",")) {
					images = images.substring(0, images.length() -1);
				}
				arr_images = images.split(",");
			}
			int user_id = getParaToInt("user_id");
			if ( user_id > 0 ) {
				Users users = Users.dao.findById(user_id);
				if (users == null) {
					users = Users.dao.findFirst("select id from users where is_true > 1 and sex = " + getPara("sex") +" and area_id = " + getPara("area_id") + " order by rand() limit 1 " );
					user_id = users.getId().intValue();
				}
			} else {
				Users users = Users.dao.findFirst("select id from users where is_true > 1 and sex = " + getPara("sex") +" and area_id = " + getPara("area_id") + " order by rand() limit 1 " );
				user_id = users.getId().intValue();
			}
			Card one = new Card();
			one.setTopicId(topic_id);
			one.setUserId(user_id);
			one.setCreateTime(SystemClock.now()/1000L);
        	one.setUpdateTime(SystemClock.now()/1000L);
        	one.setContent(content);
			one.setImgCheck(1);
			one.setIsShow(1);
			one.setIsCheck(1);
			if (new Random().nextInt(2) == 1) {
				one.setHeat(0);
			} else {
				one.setHeat(new Random().nextInt(200));
			}
			
			one.setViews(20 + new Random().nextInt(100));
			if (one.save()) {
				Long card_id = one.getId();
				if (topic != null) {
					for(String t :topic) {
						CardTopic ctopic = new CardTopic();
						ctopic.setTopicId(Integer.valueOf(t));
						ctopic.setCardId(card_id.intValue());
						ctopic.setCreateTime(SystemClock.now()/1000L);
						ctopic.setUpdateTime(SystemClock.now()/1000L);
						ctopic.save();
					}
					
				}
				if (arr_images != null) {
					for(String t :arr_images) {
						CardImg img = new CardImg();
						img.setCardId(card_id.intValue());
						img.setImageUrl(t);
						img.setCreateTime(SystemClock.now()/1000L);
						img.setUpdateTime(SystemClock.now()/1000L);
						img.setImgCheck(1);
						img.save();
					}
				}
				if (!StringUtil.empty(getPara("discusses"))) {
					String[] discusses = getPara("discusses").split(",");
					for(String dis :discusses) {
						CardDiscuss d_one = new CardDiscuss();
						String[] attr = dis.split("_");
						Users d_users = Users.dao.findFirst("select id from users where is_true > 1 and sex = " + attr[1] +" and area_id = " + attr[0] + " order by rand() limit 1 " );
						
						
						int d_user_id = d_users.getId().intValue();
						d_one.setCardId(card_id.intValue());
						d_one.setBuserId(d_user_id);
						if (attr.length == 3) {
							d_one.setUserId(d_user_id);	
						} else {
							if ("1".equals(attr[3])) {
								d_one.setUserId(user_id);	
							} else {
								d_one.setUserId(d_user_id);	
							}
						}
						
						d_one.setType(1);
						d_one.setCreateTime(SystemClock.FUTURE_TIME);
						d_one.setUpdateTime(SystemClock.FUTURE_TIME);
						d_one.setContent(StringUtil.encode(attr[2]));
						d_one.save();
					}
					
					/*
					JSONArray discusses = JSONArray.parseArray("discusses");
					for(int i = 0; i < discusses.size(); i++) {
						JSONObject discuss = discusses.getJSONObject(i);
						CardDiscuss d_one = new CardDiscuss();
						int sex = discuss.getIntValue("sex");
						int area_id = discuss.getIntValue("area_id");
						Users d_users = Users.dao.findFirst("select id from users where is_true > 1 and sex = " + sex +" and area_id = " + area_id + " order by rand() limit 1 " );
						int d_user_id = d_users.getId().intValue();
						d_one.setCardId(card_id.intValue());
						d_one.setBuserId(d_user_id);
						d_one.setUserId(d_user_id);
						d_one.setType(1);
						d_one.setCreateTime(SystemClock.now()/1000L);
						d_one.setUpdateTime(SystemClock.now()/1000L);
						d_one.setContent(StringUtil.encode(discuss.getString("content")));
						d_one.save();
					}*/
					Db.update("update card set discuss ="+discusses.length+" ,discuss_cnt = "+discusses.length+" where  id = " + card_id);
				}
			}
			renderText(APIJSON.result(0));
			
	}
	
    /**
     * 	评论动态
     *  @param card_id
     * 	@param content
     */
    public void card_discuss() {
    	String message = check_user(true);
		if (!StringUtil.empty(message)) {
			renderText(message);
		} else {
			String content =  getPara("content");
			if (StringUtil.empty(content)) {
				renderText(APIJSON.result(2001));
				return;
			}
			content = BlackList.dao.replace_content(content);
			Card card = Card.dao.findById(getPara("card_id"));
			if (card == null) {
				renderText(APIJSON.result(4000));
				return;
			}
			Long user_id = super.getUserId();
			if (UserBlack.dao.is_black(user_id.intValue(), card.getUserId())) {
				renderText(APIJSON.result(1069));
				return;
			} 
			/*
			Users users = Users.dao.findById(user_id);
			if(StringUtil.empty(users.getUserMobile())) {
				renderJson(APIJSON.result(3007));
				return;
			}
			*/
			CardDiscuss one = new CardDiscuss();
			one.setCardId(card.getId().intValue());
			one.setBuserId(user_id.intValue());
			one.setUserId(user_id.intValue());
			one.setType(1);
			one.setCreateTime(SystemClock.now()/1000L);
        	one.setUpdateTime(SystemClock.now()/1000L);
        	one.setContent(StringUtil.encode(content));
        	try {
	        	if(one.save()) {
	        		MessageUnreadNum.dao.addNum(MessageUnreadNum.MESSAGE_UNREAD_TYPE_DISCUSS, card.getUserId());
	        		CardDiscuss cnt = CardDiscuss.dao.findFirst("select count(id) as cnt from card_discuss where card_id = " + card.getId());
	            	Db.update("update card set discuss_cnt = "+ cnt.get("cnt") + ",views = views + " + new Random().nextInt(50) +"  where id = "+ card.getId());
	            	
	            	
	            	int num = new Random().nextInt(3);
	     			List<Users> usersList = Users.dao.find("SELECT t1.id FROM `users` AS t1  WHERE  t1.is_true > 1 ORDER BY RAND()  LIMIT " + num);
	     			
	     			for(int i = 0 ; i < usersList.size(); i++) {
	     				Users u = usersList.get(i);
	     				CardLike like_one = CardLike.dao.findFirst("select * from card_like where card_id = " + card.getId() +" and user_id = " + u.getId() +" order by id desc limit 1" );
	     				if (like_one == null) {
	     					like_one = new CardLike();
	     					like_one.setCardId(card.getId().intValue());
	     					like_one.set("status", 1);
	     					like_one.set("user_id", u.getId());
	     					like_one.set("is_auth", 2);
	     					like_one.setCreateTime(SystemClock.now()/1000L);
	     					like_one.setUpdateTime(SystemClock.now()/1000L);
	     					if (like_one.save()) {
	     						new com.car.action.admin.CardController().giveABPoint(u.getId().intValue());
	     						Db.update("update users set user_money = user_money + 6 where id = " +  u.getId());
	     						Db.update("update card set sort_heat = sort_heat + 6,fabulous = fabulous + 1,views = views + 20,t_views = t_views + 20 where id = " +  card.getId());
	     						MessageUnreadNum.dao.addNum(MessageUnreadNum.MESSAGE_UNREAD_TYPE_LIKE, card.getUserId());
	     					}
	     				}
	     	        } 
	            	
	        	}
        	}catch (Exception e) {
    			e.printStackTrace();
			}
			renderText(APIJSON.result(0));
		}
    }
    
    /**
     *	 回复评论的回复
     */
    public void answer_discuss() {
    	String message = check_user(true);
		if (!StringUtil.empty(message)) {
			renderText(message);
		} else {
			String content =  getPara("content");
			if (StringUtil.empty(content)) {
				renderText(APIJSON.result(2001));
				return;
			}
			content = BlackList.dao.replace_content(content);
			CardDiscuss discuss = CardDiscuss.dao.findById(getPara("discuss_id"));
			if (discuss == null) {
				renderText(APIJSON.result(4001));
				return;
			}
			Long user_id = super.getUserId();
			if (UserBlack.dao.is_black(user_id.intValue(), discuss.getUserId())) {
				renderText(APIJSON.result(1069));
				return;
			}
			/*
			Users users = Users.dao.findById(user_id);
			if(StringUtil.empty(users.getUserMobile())) {
				renderJson(APIJSON.result(3007));
				return;
			}
			*/
			CardDiscuss one = new CardDiscuss();
			one.setCardId(discuss.getCardId());
			one.setBuserId(discuss.getUserId());
			one.setUserId(user_id.intValue());
			one.set("discuss_id",getPara("discuss_id"));
			one.setType(2);
			one.setCreateTime(SystemClock.now()/1000L);
        	one.setUpdateTime(SystemClock.now()/1000L);
        	one.setContent(StringUtil.encode(content));
        	if(one.save()) {
        		Db.update("update card set discuss_cnt = discuss_cnt + 1 where id = "+ discuss.getCardId());
        		MessageUnreadNum.dao.addNum(MessageUnreadNum.MESSAGE_UNREAD_TYPE_DISCUSS, discuss.getUserId());
        	}
			renderText(APIJSON.result(0));
		}
	}
    
    /**
     * 点赞/取消点赞
     * @param card_id
     * @param token userid
     */
    public void give_like(){
    	String message = check_user(true);
		if (!StringUtil.empty(message)) {
			renderText(message);
		} else {
			Long user_id = super.getUserId();
			Card card = Card.dao.findById(getPara("card_id"));
			if (card == null) {
				renderText(APIJSON.result(2005));
				return;
			}
			CardLike like = CardLike.dao.findFirst("select * from card_like where card_id = " + card.getId() + " and " + " user_id =" + user_id);
			Users users = Users.dao.findById(user_id);
			
			if (like == null) {
				like = new CardLike();
				like.setCardId(card.getId().intValue());
				like.setStatus(1);
				like.setUserId(user_id.intValue());
				if (like.save()) {
					Db.update("update users set user_money = user_money + 6 where id = " + card.getUserId());
					MessageUnreadNum.dao.addNum(MessageUnreadNum.MESSAGE_UNREAD_TYPE_LIKE, card.getUserId());
					String sql = " set sort_heat = sort_heat + 6 , fabulous = fabulous + 1,update_time = " + SystemClock.now()/1000 ;
					if (users.getIsTrue() == 1) {
						sql = sql + ", t_fabulous = t_fabulous + 1 , tsort_heat = tsort_heat + 6 ";
					}
					Db.update("update card " + sql + " where id = " + card.getId());
					try {
						CardLike cnt = CardLike.dao.findFirst("select count(id) as cnt from card_like where card_id = " + card.getId());
	            		Db.update("update card set zan_cnt = "+ cnt.get("cnt") + " where id = "+ card.getId());
	        		}catch (Exception e) {
	        			e.printStackTrace();
					}
				}
			} else {
				// 取消赞 
				if (like.getStatus() == 1) {
					Db.update("update users set user_money = user_money - 6 where id = " + card.getUserId());
					Db.update("update card_like set status = 0 where id = " + like.getId());
					
					String sql = " set sort_heat = sort_heat - 6 , fabulous = fabulous - 1,update_time = " + SystemClock.now()/1000 ;
					if (users.getIsTrue() == 1) {
						sql = sql + ", t_fabulous = t_fabulous - 1 , tsort_heat = tsort_heat - 6 ";
					}
					Db.update("update card " + sql + " where id = " + card.getId());
					
				} else {
					Db.update("update card_like set status = 1 where id = " + like.getId());
					Db.update("update users set user_money = user_money + 6 where id = " + card.getUserId());
					MessageUnreadNum.dao.addNum(MessageUnreadNum.MESSAGE_UNREAD_TYPE_LIKE, card.getUserId());
					
					String sql = " set sort_heat = sort_heat + 6 , fabulous = fabulous + 1,update_time = " + SystemClock.now()/1000 ;
					if (users.getIsTrue() == 1) {
						sql = sql + ", t_fabulous = t_fabulous + 1 , tsort_heat = tsort_heat + 6 ";
					}
					Db.update("update card " + sql + " where id = " + card.getId());
				}
			}
			
			renderText(APIJSON.result(0));
		}
    }
    
    /**
     * 拥抱
     * @param card_id
     * @param to_user
     * @param hot_value
     * @param token userid
     */
    @Before(Tx.class)
    public void embrace() {
    	String message = check_user(true);
		if (!StringUtil.empty(message)) {
			renderText(message);
		} else {
			Long user_id = super.getUserId();
			/*
			 * if (StringUtil.empty(to_user)) {
				renderText(APIJSON.result(1050));
				return;
			}
			String to_user = getPara("to_user");
			if (Long.valueOf(to_user) == user_id ) {
				renderText(APIJSON.result(1050));
				return;
			}
			*/
			int hot_value = getParaToInt("hot_value");
			Users me = Users.dao.findById(user_id);
			if (me.getHotVal().intValue() < hot_value) {
				renderText(APIJSON.result(2003));
				return;
			}
			if (!StringUtil.empty(getPara("card_id"))) {
				Card card = Card.dao.findById(getPara("card_id"));
				if (me.getIsTrue() == 1) {
					Db.update("update card set heat = heat + " + hot_value + ", t_heat = t_heat + " + hot_value+",sort_heat= sort_heat +4,tsort_heat= tsort_heat +4 where id =" + card.getId());
				} else {
					Db.update("update card set heat = heat + " + hot_value + ", sort_heat= sort_heat +4  where id =" + card.getId());
				}
				// 帖子热度记录表
				CardHot hot = new CardHot();
				hot.setCardId(card.getId().intValue());
				hot.setHotVal(hot_value);
				hot.setType(2);
				hot.save();
				int to_user = card.getUserId();
				int half = (int)(hot_value/2);
				
				VipRecord recordMe = new VipRecord();
				recordMe.setUserId(user_id.intValue());
				recordMe.setTargetUserId(Integer.valueOf(to_user));
				recordMe.setOrderNum(Toolkit.newTradeNo());
				recordMe.setTitle("拥抱帖子" + hot_value + "℃");
				recordMe.setBuyType("User");
				recordMe.setHot(hot_value);
				recordMe.setStatus(1);
				recordMe.setOrderType(4);
				recordMe.setCreateTime(SystemClock.now()/1000L);
				recordMe.setUpdateTime(SystemClock.now()/1000L);
				recordMe.save();
				
				VipRecord recordYou = new VipRecord();
				recordYou.setUserId(Integer.valueOf(to_user));
				recordYou.setTargetUserId(user_id.intValue());
				recordYou.setOrderNum(Toolkit.newTradeNo());
				recordYou.setTitle("被拥抱" + half + "℃");
				recordYou.setBuyType("User");
				recordYou.setHot(hot_value);
				recordYou.setStatus(1);
				recordYou.setOrderType(5);
				recordYou.setCreateTime(SystemClock.now()/1000L);
				recordYou.setUpdateTime(SystemClock.now()/1000L);
				recordYou.save();
				
				Users.dao.embrace(user_id.intValue(),Integer.valueOf(to_user),hot_value);
				// 热度排名
				Db.update("update users set user_money = user_money + 4 where id = " + user_id);
				MessageUnreadNum.dao.addNum(MessageUnreadNum.MESSAGE_UNREAD_TYPE_ORDER, user_id.intValue());
				MessageUnreadNum.dao.addNum(MessageUnreadNum.MESSAGE_UNREAD_TYPE_ORDER, Integer.valueOf(to_user));
			
		        Long user_ids =  user_id;
		        String content = StringUtil.decode(me.getNickName()) + "(ID:" +user_ids+ ")拥抱了你的帖子" + half +"℃, 收到新的打赏";
				RongCloudUtil.sendTxt("141", String.valueOf(to_user), content);
			}
			renderText(APIJSON.result(0));
		}
    }
    
    /**
     * 点赞的人
     * @param card_id
     * @param type
     */
    public void like_person() {
    	String message = check_user(true);
		if (!StringUtil.empty(message)) {
			renderText(message);
		} else {
			String card_id = getPara("card_id");
			String type    = getPara("type");
			 
			ArrayList<Map<String,Object>> result = CardLike.dao.sel_like_person(card_id , type);
			renderText(APIJSON.send(result));
		}
    }
    
    
    /**
    * 收藏/取消收藏
    * @param card_id
    * @param token userid
    */
   public void card_collect(){
   		String message = check_user(true);
		if (!StringUtil.empty(message)) {
			renderText(message);
		} else {
			Long user_id = super.getUserId();
			Card card = Card.dao.findById(getPara("card_id"));
			if (card == null) {
				renderText(APIJSON.result(2005));
				return;
			}
			CardCollect collect = CardCollect.dao.findFirst("select * from card_collect where card_id = " + card.getId() + " and " + " user_id =" + user_id);
			if (collect == null) {
				collect = new CardCollect();
				collect.setCardId(card.getId().intValue());
				collect.setStatus(1);
				collect.setUserId(user_id.intValue());
				collect.setCreateTime(SystemClock.now()/1000L);
				collect.setUpdateTime(SystemClock.now()/1000L);
				collect.save();
			} else {
				// 取消
				if (collect.getStatus() == 1) {
					Db.update("update card_collect set status = 0 where id = " + collect.getId());
					
				} else {
					Db.update("update card_collect set status = 1 where id = " + collect.getId());
				}
			}
			
			renderText(APIJSON.result(0));
		}
   	}
   
   /**
    * 举报动态
    */
   public void card_report(){
	   	String message = check_user(true);
		if (!StringUtil.empty(message)) {
			renderText(message);
		} else {
			
			Card card = Card.dao.findById(getPara("card_id"));
			if (card == null) {
				renderText(APIJSON.result(2005));
				return;
			}
			ReportCards one = ReportCards.dao.findFirst("select * from report_cards where status =1 and report_card_id = "+ getPara("card_id") );
			if (one == null) {
				Long user_id = super.getUserId();
				one = new ReportCards();
				one.setReportCardId(card.getId().intValue());
				one.setUserId(user_id.intValue());
				one.setStatus(1);
				one.save();
			}
			renderText(APIJSON.send(0,null ,"举报成功，客服会24小时之内做出对应处理!"));
		}
	}
   
   /**
    * 拉黑
    */
   public void card_black(){
	   	String message = check_user(true);
		if (!StringUtil.empty(message)) {
			renderText(message);
		} else {
			
			Card card = Card.dao.findById(getPara("card_id"));
			if (card == null) {
				renderText(APIJSON.result(2005));
				return;
			}
			Long user_id = super.getUserId();
			UserBlack one = UserBlack.dao.findFirst("select id,status,user_id,black_user_id from user_black where user_id = " + user_id + " and black_user_id = " + card.getUserId());
	        if (one == null) {
	        	one = new UserBlack();
	        	one.set("user_id", user_id);
	        	one.set("black_user_id", card.getUserId());
	        	one.set("status", 1);
	        	one.set("create_time", SystemClock.now()/1000L);
	        	if (one.save()) {
	        		Map<String,String> params = Maps.newHashMap();
		        	params.put("userId", String.valueOf(user_id));
		        	params.put("blackUserId", getPara("black_user_id"));
		        	RongCloudUtil.post("http://api-cn.ronghub.com/user/blacklist/add.json", params);
	        	}
	        }
			renderText(APIJSON.result(0));
		}
	}
   
   /**
    * 本周最热
    * author:Lemon
    */
   public void week_hotest() {
		Long user_id = super.getUserId();
		int page = 1;
        if(!StringUtil.empty(getPara("page"))) {
        	page = getParaToInt("page");
        }
        
        String cache_key = "card__week_hotest_"+ user_id + "__" + page;
        ArrayList<Map<String,Object>> result = CacheKit.get("api", cache_key);
        
        if (result == null) {
        	result = Card.dao.week_hotest(user_id , page);
        	CacheKit.put("api", cache_key, result);
        }
        renderJson(APIJSON.sendJSON(result));
   }
   
    /**
     * 	关注的人
     */
	public void focus() {
		String message = check_user(true);
		if (!StringUtil.empty(message)) {
			renderText(message);
		} else {
			int page = 1;
	        if(!StringUtil.empty(getPara("page"))) {
	        	page = getParaToInt("page");
	        }
			ArrayList<Map<String,Object>> result = UserFans.dao.focus(getUserId(), page);
	        renderJson(APIJSON.sendJSON(result));
		}
	}
	
	/**
     * 关注/取消关注圈子
     */
	public void unable_focus() {
		String message = check_user(true);
		if (!StringUtil.empty(message)) {
			renderText(message);
		} else {
			Long user_id = super.getUserId();
			int circle_id = getParaToInt("circle_id");
			CircleLike like = CircleLike.dao.findFirst("select * from circle_like where circle_id = " + circle_id + " and " + " user_id =" + user_id);
			if (like == null) {
				like = new CircleLike();
				like.setCircleId(circle_id);
				like.setStatus(1);
				like.setUserId(user_id.intValue());
				like.save();
			} else {
				// 取消
				if (like.getStatus() == 1) {
					Db.update("update circle_like set status = 0 where id = " + like.getId());
					
				} else {
					Db.update("update circle_like set status = 1 where id = " + like.getId());
				}
			}
			
			renderText(APIJSON.result(0));
		}
		
	}
	
	/**
	 * 删帖
	 * @card_id
	 */
	public void delete_card() {
		String message = check_user(true);
		if (!StringUtil.empty(message)) {
			renderText(message);
		} else {
			Long user_id = super.getUserId();
			Db.delete("delete from card where id = " + getPara("card_id") +" and user_id = " + user_id);
			Db.delete("delete from card_discuss where card_id = " + getPara("card_id") +" and user_id = " + user_id);
			renderText(APIJSON.result(0));
		}
	}
	
}
